#pragma comment(linker, "/STACK:10000000")
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <set>
#include <map>
#include <cstdio>
#include <cassert>
#include <string>
#include <cstring>
#define ldb long double
#define ll long long
#define fi first
#define se second
#define fill(a, c) memset(a, c, sizeof(a))
#define sqr(a) ((a) * (a))
#define nextLine() {int c = 0; while((c = getchar()) != 10 && c != EOF);}
#define getBit(mask, k) (((mask) / pw[k]) % pw[1])
#define setBit(mask, k, l) (((mask) / pw[k + 1] * pw[1] + (l)) * pw[k] + ((mask) % pw[k]))
#define debug(a) cerr << #a << " = " << a << " ";
#define debugl(a) cerr << #a << " = " << a << "\n";
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define ff first
#define ss second 
const ldb LDINF = 9128739847123.00;
const ldb eps = 1e-9;
const int inf = 1 << 28;
const ldb pi = fabsl(atan2l(0.0, -1.0));
using namespace std;

int A, B, X, Y, Z, P, Q, C;
int a[3][3];
int d[3][3];
int t[3][3];

void load()
{
	cin >> A >> B >> X >> Y >> Z >> P >> C >> Q;
}

inline int get(int k)
{
	a[0][0] = 0;
	a[0][1] = Y;
	a[0][2] = 0;
	a[1][0] = 1;
	a[1][1] = X;
	a[1][2] = 0;
	a[2][0] = 0;
	a[2][1] = Z;
	a[2][2] = 1;
	memset(d, 0, sizeof d);
	for (int i = 0; i < 3; i++)
		d[i][i] = 1;
	for (;k; k >>= 1)
	{
		if (k & 1)
		{
			for (int i = 0; i < 3; i++)
				for (int j = 0; j < 3; j++)
				{
					t[i][j] = 0;
					for (int k = 0; k < 3; k++)
					{
						t[i][j] += (d[i][k] * a[k][j]) % P;
						if (t[i][j] >= P) t[i][j] -= P;
					}
				}
			memcpy(d, t, sizeof(int) * 9);
		}
		for (int i = 0; i < 3; i++)
				for (int j = 0; j < 3; j++)
				{
					t[i][j] = 0;
					for (int k = 0; k < 3; k++)
					{
						t[i][j] += (a[i][k] * a[k][j]) % P;
						if (t[i][j] >= P) t[i][j] -= P;
					}
				}
			memcpy(a, t, sizeof(int) * 9);
	}
	return (A * d[0][0] + B * d[1][0] + d[2][0]) % P;
}

vector <int> pred, cycle;


inline int findL(vector <int> &a, int key)
{
	int l = 0;
	int r = a.size() - 1;
	int mid, res = -1;
	while (l <= r)
	{
		mid = (l + r) >> 1;
		if (a[mid] >= key)
		{
			res = mid;
			r = mid - 1;
		}
		else l = mid + 1;
	}
	return res;
}

inline int findR(vector <int> &a, int key)
{
	int l = 0;
	int r = a.size() - 1;
	int mid, res = -1;
	while (l <= r)
	{
		mid = (l + r) >> 1;
		if (a[mid] <= key)
		{
			res = mid;
			l = mid + 1;
		}
		else r = mid - 1;
	}
	return res;
}

void solve()
{
	int pred_period, period;
	int f1 = get(P * P + 1), f2 = get(P * P + 2);
	cycle.resize(0);
	pred.resize(0);
	int _1, _2, _3;
	_1 = f1;
	_2 = f2;

	for (int i = P * P + 3; ; i++)
	{
		_3 = (_1 * Y + _2 * X + Z) % P;
		_1 = _2;
		_2 = _3;
		if (_1 == f1 && _2 == f2)
		{
			period = i - P * P - 2;
			break;
		}
		if (period > 30000000)
		{
			cerr << period;
			throw 42;
		}
   	}
//   	cerr << "period = " << period << "\n";
//   	cerr << f1 << " " << f2 << "\n";
  // 	cerr << get(P * P + 1 + period) << " " << get(P * P + 2 + period) << "\n";
   	_1 = A;
   	_2 = B;
   	int __1 = get(period);
   	int __2 = get(period + 1);
   	for (pred_period = 0; _1 != __1 || _2 != __2; pred_period++)
   	{
   		_3 = (_1 * Y + _2 * X + Z) % P;
   		_1 = _2;
   		_2 = _3;
   		_3 = (__1 * Y + __2 * X + Z) % P;
   		__1 = __2;
   		__2 = _3;
   		if (pred_period > 1000000)
   		{
   			throw 42;
   		}
   	}
   	

   //	cerr << pred_period << " " << period << "\n";
//   	cerr << "A = " << A << " " << B << " " << C << "\n";
  	if (A == C && pred_period > 0)
   		pred.pb(0);
   	if (B == C && pred_period > 1)
   		pred.pb(1);
   	_1 = A;
   	_2 = B;
   	for (int i = 2; i < pred_period; i++)
   	{
   		_3 = (_1 * Y + _2 * X + Z) % P;
   		if (_3 == C)
   			pred.pb(i);
   		_1 = _2;
   		_2 = _3;		
   	}
   	int i = 0;
   	if (pred_period == 0)
   	{
   		if (A == C) cycle.pb(0);
   		if (B == C && period > 1) cycle.pb(1);
   		i = 2;
   	}
   	else if (pred_period == 1)
   	{
   		if (B == C) cycle.pb(0);
   		i = 1;
   	}
   	for (; i < period; i++)
   	{
   		_3 = (_1 * Y + _2 * X + Z) % P;
   		if (_3 == C)
   			cycle.pb(i);
   		_1 = _2;
   		_2 = _3;
   	}
//   	cerr << "ok\n";

  // 	cerr << "\n";
  // 	cerr << "cycle(" << period << "): ";
  // 	for (int i = 0; i < cycle.size(); i++)
 //  	{
 //  		cerr << cycle[i] << " ";
 //  	}
 //  	cerr << "\n";

 //	cerr << cycle.size() << "\n";
	ll l, r, res;
	for (int i = 0; i < Q; i++)
	{
		scanf("%lld%lld", &l, &r);
		l--;
		r--;
		res = 0; 
		if (l < pred_period && r < pred_period)
		{
			int t1 = findL(pred, l);
			int t2 = findR(pred, r);
			if (t1 != -1 && t2 != -1 && t2 - t1 >= 0)
				res += t2 - t1 + 1;
			l = pred_period;
			r = pred_period - 1;
		}
		if (l < pred_period)
		{
			int t1 = findL(pred, l);
			if (t1 != -1)
				res += pred.size() - t1;
			l = pred_period;
		}
		l -= pred_period;
		r -= pred_period;
	//	cerr << l << " " << r << "\n";
		if (l <= r) 
		{
			ll t1 = l / period;
			ll t2 = r / period;
			int p = findL(cycle, l % period);
			int q = findR(cycle, r % period);
		//	cerr << "answering " << t1 << " " << t2 << " " << p << " " << q << "\n";
			if (t1 == t2)
			{
				if (p == -1) p = cycle.size();
				if (p <= q)
					res += (q - p + 1);
			}
			else 
			{
				if (p != -1) res += cycle.size() - p;
				if (q != -1) res += q + 1;
				t1++;
				t2--;
				res += cycle.size() * (t2 - t1 + 1);
			}
		}
		printf("%lld\n", res);
	}	
}

#define file "lucky"
int main()
{
	#ifndef ONLINE_JUDGE
		freopen(file".in", "rt", stdin);
		freopen(file".out", "wt", stdout);
	#endif
	int T;
	cin >> T;
	for (int i = 0; i < T; i++)
	{
		load();
		solve();
	}
	return 0;
}
